package com.vhall.component.controller.data;

import com.vhall.component.controller.data.facade.DataFacade;
import com.vhall.component.controller.room.facade.RoomInnerFacade;
import com.vhall.component.entity.data.vo.*;
import com.vhall.component.entity.page.vo.PageVO;
import com.vhall.component.entity.room.dto.RoomsEntityDTO;
import com.vhall.component.entity.tag.vo.PageChannelListRspVO;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.annotation.VssTokenAuth;
import com.vhall.component.plugin.common.constant.ConstStr;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.service.data.DataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author guoxin.wang
 * @date 2021-06-18 10:36
 */
@VhallComponent
public class DataComponent implements DataFacade {

    @Autowired
    private DataService dataService;

    @Autowired
    private RoomInnerFacade roomInnerFacade;

    //    @TokenAuth
    @Override
    public BasicStatisticsInfoVO getBasicStatisticsInfo(UserParamVO reqVO) {
        return dataService.getBasicInfo(reqVO);
    }

    //    @TokenAuth
    @Override
    public List<UserDeviceRspVO> getDeviceInfo(UserParamVO reqVO) {
        reqVO.setEndTime(setReqEndTime(reqVO.getEndTime()));
        String status = reqVO.getStatus();
        List<UserDeviceRspVO> udrv = new ArrayList<>();
        if (StringUtils.isBlank(reqVO.getRoomId())) {
            reqVO.setIlId(null);
        } else {
            RoomsEntityDTO roomInfo = roomInnerFacade.getRoomInfo(reqVO.getRoomId());
            if (roomInfo == null) {
                reqVO.setIlId(null);
            } else {
                reqVO.setIlId(roomInfo.getIlId());
            }
        }
        if ("1".equals(status)) {
            udrv = dataService.getDeviceInfo(reqVO);
        } else if ("2".equals(status)) {
            roomCheck(reqVO);
            udrv = dataService.getLiveDeviceInfo(reqVO);
        } else if ("3".equals(status)) {
            roomCheck(reqVO);
            udrv = dataService.getVodDeviceInfo(reqVO);
        }
        return udrv;
    }

    private void roomCheck(UserParamVO reqVO) {
        if (reqVO.getIlId() == null || StringUtils.isBlank(reqVO.getIlId().toString())) {
            throw new BusinessException(BizErrorCode.BIZ_INVALID_PARAM);
        }
        RoomsEntityDTO ilInfo = roomInnerFacade.getRoomInfoByIlId(reqVO.getIlId().toString());
        if (null == ilInfo) {
            throw new BusinessException(BizErrorCode.EMPTY_ROOM);
        }
    }

    //    @TokenAuth
    @Override
    public List<UserProvinceRspVO> getProvinceInfo(UserParamVO reqVO) {
        reqVO.setEndTime(setReqEndTime(reqVO.getEndTime()));
        String status = reqVO.getStatus();
        setIlId(reqVO);
        List<UserProvinceRspVO> uprv = new ArrayList<>();
        if ("1".equals(status)) {
            uprv = dataService.getProvinceInfo(reqVO);
        } else if ("2".equals(status)) {
            roomCheck(reqVO);
            uprv = dataService.getLiveProvinceInfo(reqVO);
        } else if ("3".equals(status)) {
            roomCheck(reqVO);
            uprv = dataService.getVodProvinceInfo(reqVO);
        }
        for (UserProvinceRspVO uv : uprv) {
            if (StringUtils.isBlank(uv.getName())) {
                uv.setName("其他");
            }
        }
        return uprv;
    }

    //    @TokenAuth
    @Override
    public List<UserLiveWatchUvRspVO> getUserWatchUvInfo(UserParamVO reqVO) {
        reqVO.setEndTime(setReqEndTime(reqVO.getEndTime()));
        String status = reqVO.getStatus();
        setIlId(reqVO);
        List<UserLiveWatchUvRspVO> uprv = new ArrayList<>();
        if ("1".equals(status)) {
            uprv = dataService.getUserWatchUvInfo(reqVO);
        } else if ("2".equals(status)) {
            roomCheck(reqVO);
            uprv = dataService.getLiveUserWatchUvInfo(reqVO);
        } else if ("3".equals(status)) {
            roomCheck(reqVO);
            uprv = dataService.getVodUserWatchUvInfo(reqVO);
        }
        return uprv;
    }

    private void setIlId(UserParamVO reqVO) {
        if (StringUtils.isBlank(reqVO.getRoomId())) {
            reqVO.setIlId(null);
        } else {
            RoomsEntityDTO roomInfo = roomInnerFacade.getRoomInfo(reqVO.getRoomId());
            if (roomInfo == null) {
                reqVO.setIlId(null);
            } else {
                reqVO.setIlId(roomInfo.getIlId());
            }
        }
    }

    //    @TokenAuth
    @Override
    public List<UserWatchPvRspVO> getUserWatchPvInfo(UserParamVO reqVO) {
        reqVO.setEndTime(setReqEndTime(reqVO.getEndTime()));
        if (StringUtils.isBlank(reqVO.getRoomId())) {
            reqVO.setIlId(null);
        } else {
            RoomsEntityDTO roomInfo = roomInnerFacade.getRoomInfo(reqVO.getRoomId());
            if (roomInfo == null) {
                reqVO.setIlId(null);
            } else {
                reqVO.setIlId(roomInfo.getIlId());
            }
        }
        return dataService.getUserWatchPvInfo(reqVO);
    }
    @TokenAuth
    @Override
    public NumOfViewersAndOtherInfoRspVO numOfViewersAndOtherInfo(UserParamVO reqVO) {
        return dataService.numOfViewersAndOtherInfo(reqVO);
    }

    @Override
    public List<PageChannelListRspVO> getDpChannelAvgViewDuration(UserParamVO reqVO) {
        return dataService.getDpChannelAvgViewDuration(reqVO);
    }

    @Override
    public List<DpLecturerAvgViewDurationRspVO> getDpLecturerAvgViewDuration(UserParamVO reqVO) {
        return dataService.getDpLecturerAvgViewDuration(reqVO);
    }

    @TokenAuth
    @Override
    public PageVO<DpLiveViewDataResponseVO> getDpColumnLiveViewData(UserParamVO reqVO) {
        return dataService.getDpColumnLiveViewData(reqVO);
    }

    @TokenAuth
    @Override
    public PageVO<DpLiveViewDataResponseVO> getDpAnchorLiveViewData(UserParamVO reqVO) {
        return dataService.getDpAnchorLiveViewData(reqVO);
    }
    @TokenAuth
    @Override
    public List<DpWatchTimeTopRspVO> getDpWatchTimeTop(UserParamVO reqVO) {
        return dataService.getDpWatchTimeTop(reqVO);
    }
    @TokenAuth
    @Override
    public List<UserViewDurationDistributionRspVO> getDpUserViewDurationDistribution(UserParamVO reqVO) {
        return dataService.getDpUserViewDurationDistribution(reqVO);
    }

    @Override
    @VssTokenAuth
    public Long recordViewingInfo(RecordViewingInfoVO reqVO) {

        return dataService.virtualPv(reqVO.getRoomId());
    }

    @Override
    public List<PlayVolumeTrendRspVO> playVolumeTrend(UserParamVO reqVO) {
        return dataService.playVolumeTrend(reqVO);
    }

    @Override
    public List<SelectTimeIntervalOptionsVO> getSelectTimeIntervalOptions() {
        return dataService.getSelectTimeIntervalOptions();
    }

    public String setReqEndTime(String endTime) {
        if (StringUtils.isNotBlank(endTime)) {
            if (endTime.contains(ConstStr.TIME_ZERO)) {
                endTime = endTime.replace(ConstStr.TIME_ZERO, ConstStr.TIME_FULL);
            } else {
                endTime = endTime + " " + ConstStr.TIME_FULL;
            }
        }
        return endTime;
    }

}
